<?php

//dodawanie znaku wodnego
function watermark_add() {
	global $conf, $lang;

	if(!perms_check('images', 'upload')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	//file name
	if(!$_POST['name']) {
		if($_POST['how_image'] == 'form')
			$_POST['name'] = basename($_FILES['image_form']['name']);
		elseif($_POST['how_image'] == 'link')
			$_POST['name'] = end($foo = explode('/', $_POST['image_link']));
	}
	$_POST['name'] = read_text_filename($_POST['name']);
	//

	//file extension
	if(strpos($_POST['name'], '.')===false) {
		if($_POST['how_image'] == 'form')
			$_POST['name'] .= '.'.end($foo = explode('.', $_FILES['image_form']['name']));
		elseif($_POST['how_image'] == 'link')
			$_POST['name'] .= '.'.end($foo = explode('.', $_POST['image_link']));				
	}
	//
	
	if(($_POST['how_image'] == 'form' and filesize($_FILES['image_form']['tmp_name']) == 0)) {
		redirect($_SESSION['redirect_1']);
		exit();
	}
	elseif(file_exists($conf['watermarks_dir'].$_POST['name'])) {
		redirect('index.php?module=error&error=image_error3');
		exit();
	}
	
	$fext = end($foo = explode(".", $_POST['name']));
	if(!eregi("^(gif|jpg|jpeg|png)$", $fext)) {
		redirect('index.php?module=error&error=image_error4');
		exit($_POST['name']);
	}

	//upload
	if($_POST['how_image'] == 'link')
		upload_image_from_url($_POST['image_link'], $conf['watermarks_dir'].$_POST['name']);
	elseif($_POST['how_image'] == 'form')
		upload_image_form($_FILES['image_form'], $conf['watermarks_dir'].$_POST['name']);
		
	list($width, $height, $value, $params) = GetImageSize($conf['watermarks_dir'].$_POST['name']);
	$fn = explode(".", $_POST['name']);
	
	$db = new dbquery;
	$db->query("INSERT INTO $conf[prefix]watermark VALUES(NULL, '$fn[0]', '".$_POST['name']."', $width, $height)") or $db->err(__FILE__, __LINE__);

	redirect('index.php?module=admin&action=watermark');
	exit;
}

function watermark_edit() {
	global $conf, $lang;

	SQLvalidate($_POST['id']);

	if(!perms_check('images', 'edit')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]watermark WHERE id = $_POST[id] LIMIT 0, 1") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object();

	if($db->num_rows()==0) {
		redirect($_SESSION['redirect_1']);
		exit();
	}	
	
	//file name
	if(!$_POST['name']) {
		if($_POST['how_image'] == 'form')
			$_POST['name'] = basename($_FILES['image_form']['name']);
		elseif($_POST['how_image'] == 'link')
			$_POST['name'] = end($foo = explode('/', $_POST['image_link']));
	}
	$_POST['name'] = read_text_filename($_POST['name']);
	//

	//file extension
	if(strpos($_POST['name'], '.')===false) {
		if($_POST['how_image'] == 'form') {
			$_POST['name'] .= '.'.end($foo = explode('.', $_FILES['image_form']['name']));
			$fext=end($foo = explode('.', $_FILES['image_form']['name']));
		}
		elseif($_POST['how_image'] == 'link') {
			$_POST['name'] .= '.'.end($foo = explode('.', $_POST['image_link']));
			$fext=end($foo = explode('.', $_POST['image_link']));
		}
		else {
		    $_POST['name'] .= '.'.end($foo = explode('.', $_POST['filename']));
		    $fext=end($foo = explode('.', $_POST['filename']));
		}
	} else $fext=end($foo = explode('.', $_POST['filename']));
    
	$ext=end($foo = explode('.', $_POST['name']));
	
	if($fext != $ext) {
		redirect('index.php?module=error&error=image_error5');
		exit();
	}
	if(!eregi("^(gif|jpg|jpeg|png)$", $ext)) {
		redirect('index.php?module=error&error=image_error4');
		exit();
	}
	//
	
	if(($_POST['how_image'] == 'form' and filesize($_FILES['image_form']['tmp_name']) == 0)) {
		redirect($_SESSION['redirect_1']);
		exit();
	}
	if(file_exists($conf['watermarks_dir'].$_POST['name']) && $_POST['filename'] != $_POST['name']) {
		redirect('index.php?module=error&error=image_error3');
		exit();
	}

	//zmiana nazwy
	if($_POST['name']!=$d->fname && $_POST['how_image']=='no_change')
		rename($conf['watermarks_dir'].$d->fname, $conf['watermarks_dir'].$_POST['name']);
    else
		unlink($conf['watermarks_dir'].$d->fname);

	if($_POST['how_image'] != 'no_change') {
		if($_POST['how_image'] == 'link')
			upload_image_from_url($_POST['image_link'], $conf['watermarks_dir'].$_POST['name']);
		elseif($_POST['how_image'] == 'form')
			upload_image_form($_FILES['image_form'], $conf['watermarks_dir'].$_POST['name']);
    }
	
	list($width, $height, $value, $params) = GetImageSize($conf['watermarks_dir'].$_POST['name']);
	$fn = explode(".", $_POST['name']);
	
	$db->query("UPDATE $conf[prefix]watermark SET name = '$fn[0]', fname = '$_POST[name]', x = $width, y = $height WHERE id=$_POST[id]") or $db->err(__FILE__, __LINE__);

	redirect('index.php?module=admin&action=watermark');
	exit;
}


function watermark_del() {
	global $conf;

	SQLvalidate($_GET['id']);

	if(!perms_check('images', 'del')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]watermark WHERE id = $_GET[id]") or $db->err(__FILE__, __LINE__);

	$tmp = $db->fetch_object();
	@unlink($conf['watermarks_dir'].$tmp->fname);

	$db->query("DELETE FROM $conf[prefix]watermark WHERE id = $_GET[id]") or $db->err(__FILE__, __LINE__);

	redirect('index.php?module=admin&action=watermark');
	exit;
}
?>
